﻿using System;
using System.Collections.Generic;
using System.Linq;

using Xc.DataAccess.Core.Extensions;
namespace Xc.DataAccess.PcpChannels
{
    /// <summary>
    /// 设备请求升级包
    /// </summary>
    public class DevicePackageRespond : RequestFrame
    {
        public DevicePackageRespond(PackageRequest status, ushort packageIndex, byte[] packageData)
        {
            Status = status;
            PackageIndex = packageIndex;
            PackageData = packageData;
        }

        public override MessageCode MessageCode => MessageCode.PackageRequest;
        /// <summary>
        /// 状态码
        /// </summary>
        public PackageRequest Status { get; set; }
        /// <summary>
        /// 分片序号
        /// </summary>
        public ushort PackageIndex { get; set; }
        /// <summary>
        /// 分片数据
        /// </summary>
        public byte[] PackageData { get; set; }
        /// <summary>
        /// 分片数据
        /// </summary>
        public override byte[] DataArea
        {
            get
            {
                List<byte> vs = new List<byte>();
                vs.Add((byte)Status);
                vs.AddRange(PackageIndex.FromUInt16BigEndian());
                if (PackageData != null)
                {
                    vs.AddRange(PackageData);
                }

                return vs.ToArray();
            }
        }
        public enum PackageRequest
        {
            /// <summary>
            /// 处理成功
            /// </summary>
            OK = 0,
            /// <summary>
            /// 升级任务不存在
            /// </summary>
            NotExist = 0x80,
            /// <summary>
            /// 指定分片不存在
            /// </summary>
            IndexNotExist = 0x81
        }
    }
}
